package com.itheima.boottlias.controller;

import com.itheima.boottlias.domain.pojo.Emp;
import com.itheima.boottlias.domain.query.QueryParam;
import com.itheima.boottlias.domain.vo.PageResult;
import com.itheima.boottlias.domain.vo.Result;
import com.itheima.boottlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @since 2025-07-11 14:56
 */
@RestController
@RequestMapping("/emps")
@Slf4j
@CrossOrigin
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result pageQuery(QueryParam queryParam){
        log.info("queryParam:{}", queryParam);
        PageResult<Emp> pageResult = empService.pageQuery(queryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody Emp emp) throws Exception {
        log.info("{}", emp);
        //调用业务
        empService.addEmpAndExprList(emp);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteByIds(Integer[] ids) throws Exception {
        empService.deleteByIds(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result findById(@PathVariable(name = "id") Integer id){
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }

    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        List<Emp> emps = empService.list();
        return Result.success(emps);
    }

}
